using System;
using System.Collections.Generic;
using UnityEngine;

namespace Framework
{
    public class BindActionCallback : IActionInputCallback
    {
        private readonly Dictionary<string, Action<InputAction>> startMap = new Dictionary<string, Action<InputAction>>();
        private readonly Dictionary<string, Action<InputAction>> performMap = new Dictionary<string, Action<InputAction>>();
        private readonly Dictionary<string, Action<InputAction>> cancelMap = new Dictionary<string, Action<InputAction>>();
        private readonly Dictionary<string, Action<InputAction>> holdMap = new Dictionary<string, Action<InputAction>>();
        public void Clear()
        {
            startMap.Clear();
            performMap.Clear();
            cancelMap.Clear();
            holdMap.Clear();
        }
        public void SetStartCallback(string actionName, Action<InputAction> value)
        {
            if (!startMap.ContainsKey(actionName))
            {
                startMap.Add(actionName, value);
            }
            else
            {
                startMap[actionName] = value;
            }
        }
        public void SetPerformCallback(string actionName, Action<InputAction> value)
        {
            if (!performMap.ContainsKey(actionName))
            {
                performMap.Add(actionName, value);
            }
            else
            {
                performMap[actionName] = value;
            }
        }
        public void SetCancelCallback(string actionName, Action<InputAction> value)
        {
            if (!cancelMap.ContainsKey(actionName))
            {
                cancelMap.Add(actionName, value);
            }
            else
            {
                cancelMap[actionName] = value;
            }
        }
        public void SetHoldCallback(string actionName, Action<InputAction> value)
        {
            if (!holdMap.ContainsKey(actionName))
            {
                holdMap.Add(actionName, value);
            }
            else
            {
                holdMap[actionName] = value;
            }
        }
        public void OnActionHoldPerform(InputAction action)
        {
            if (holdMap.TryGetValue(action.name, out var callback))
            {
                callback.Invoke(action);
            }
        }

        public void OnActionInputCancel(InputAction action)
        {
            if (cancelMap.TryGetValue(action.name, out var callback))
            {
                callback.Invoke(action);
            }
        }

        public void OnActionInputPerform(InputAction action)
        {
            if (performMap.TryGetValue(action.name, out var callback))
            {
                callback.Invoke(action);
            }
        }

        public void OnActionInputStart(InputAction action)
        {
            if (startMap.TryGetValue(action.name, out var callback))
            {
                callback.Invoke(action);
            }
        }
    }
}